#include "time_manager.h"
extern __IO uint32_t u32SystemTick100us;
uint32_t adc1_times = 0, adc_0_5s_times = 0;
__IO uint16_t u16SystemTime100us;
__IO uint16_t u16SystemTime200us;
__IO uint16_t u16SystemTime1ms;
__IO uint16_t u16SystemTime2ms;
__IO uint16_t u16SystemTime10ms;
__IO uint16_t u16SystemTime50ms;
__IO uint16_t u16SystemTime100ms;
__IO uint16_t u16SystemTime500ms;
extern pole_status pole_status_set;
extern uint32_t u16ADC1_16in1_adc[10];
extern uint32_t u16ADC1_4in1_adc[10];
char motor_run_mode = 0, get_status_time = 0;
uint32_t speed_run_real = 0;
void System_Time_Management(void)
{
    //    TIM_BDTRInitTypeDef TIM_BDTRInitStruct; // 20201027

    //    uint16_t u16TempBDTRStatusRead; // 20201027
    if (u32SystemTick100us > 0) // 100us loop, 100us executed once in this loop
    {
        if (motor_run_mode == 2)
        {
        }

        speed_control_watching();
        u32SystemTick100us--;
        u16SystemTime100us++;
    }
    if (u16SystemTime100us > 2) // 200us loop, 200us executed once in this loop
    {

        u16SystemTime100us -= 2;
        u16SystemTime200us++;
    }
    if (u16SystemTime200us > 5) // 1ms loop, 1ms executed once in this loop
    {
        u16SystemTime200us -= 5;
        u16SystemTime1ms++;
    }
    else if (u16SystemTime1ms > 2) // 2ms loop, 2ms executed once in this loop
    {

        u16SystemTime1ms -= 2;
        u16SystemTime2ms++;
    }
    else if (u16SystemTime2ms > 5) // 10ms loop, 10ms executed once in this loop
    {
        if (pole_status_set.u_pole_status == 2)
        {
            // printf("adc:%x", u16ADC1_4in1_adc[0]);
        }

        u16SystemTime2ms -= 5;
        u16SystemTime10ms++;
    }
    else if (u16SystemTime10ms > 5) // 50ms loop,  50ms executed once in this loop
    {
        u16SystemTime10ms -= 5;
        u16SystemTime50ms++;
    }
    else if (u16SystemTime50ms > 2) // 100ms loop, 100ms executed once in this loop
    {
        speed_run_real = get_speed_now();
        if (speed_run_real > 40)
        {
            //  BLDC_motor_modulation_value_set(100);
        }
        else
        {
            BLDC_motor_modulation_value_set(80);
        }

        u16SystemTime50ms -= 2;
        u16SystemTime100ms++;
    }
    else if (u16SystemTime100ms > 5) // 500ms loop,  500ms executed once in this loop
    {
        u16SystemTime100ms -= 5;
        u16SystemTime500ms++;
        printf("adc1_times:%d\n", adc1_times);
        BLDC_change_mode_compare();
        adc_0_5s_times = adc1_times;
        adc1_times = 0;
        LED2_TOGGLE();
    }
    else if (u16SystemTime500ms > 2)
    {
        switch (motor_run_mode)
        {
        case 1:
            motor_status_set(0, 2, 4);
            get_status_time = 1;
            motor_run_mode = 2;
            set_speed(10);
            break;
        case 2:
            if (get_status_time-- <= 0)
            {
                motor_run_mode = 3;
                set_speed(200);
            }
            break;
        case 3:
            BLDC_motor_modulation_value_set(100);
            break;
        default:
            break;
        }

        u16SystemTime500ms -= 4;
        // motor_status_change_auto();
    }
}
void set_motor_start(void)
{
    motor_run_mode = 1;
}
